package org.contextsensitivetodo.context.rules;

import java.util.LinkedList;

import android.util.Log;

public abstract class SuperContextRule implements ContextRule {

	private LinkedList<ContextRuleStateChangeListener> listeners = new LinkedList<ContextRuleStateChangeListener>();
	private boolean currentState = false;
	
	
	public boolean isOn() {
		return this.currentState;
	}
	
	
	public void setNewState(boolean newState){
		Log.d("cstodo", "new state, setting to" + newState);
		if (newState != this.currentState) {
			this.currentState = newState;
			this.fireEvent(!this.currentState, this.currentState);
		}
	}
	
	
	public void addContextRuleStateChangeListener(
			ContextRuleStateChangeListener listener) {
		this.listeners.add(listener);

	}

	public void removeContextRuleStateChangeListener(
			ContextRuleStateChangeListener listener) {
		this.listeners.remove(listener);
	}

	public void fireEvent(boolean oldState, boolean newState) {
		Log.d("cstodo", "*** User Context rule state change detected!");
		
		ContextRuleStateChangedEvent event = new ContextRuleStateChangedEvent(this, oldState, newState);
		
		for (ContextRuleStateChangeListener listener : this.listeners) {
			listener.handleContextRuleStateChanged(event);
		}
	}
	
	
}
